﻿using System;
using System.Data;
using TopBuilds.Database;

namespace TopBuilds.Restaurant.Data
{
    public class ProductOptionCategoryObject
    {
        public static ProductOptionCategory[] GetProductOptionCategories()
        {
            return Query(null);
        }

        public static ProductOptionCategory GetProductOptionCategory(int id)
        {
            ProductOptionCategory productOptionCategory = new ProductOptionCategory();

            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(ProductOptionCategoryDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = ProductOptionCategoryDesc.TableName;
            string sql = builder.QueryCommandLine;

            DataTable productOptionCategories = DatabaseObject.Instance.ExecuteQuerySQL(sql);

            productOptionCategory.Id                          = Convert.ToInt32(productOptionCategories.Rows[0][ProductOptionCategoryDesc.Id]);
            productOptionCategory.ProductOptionCagegoryId     = Convert.ToInt32(productOptionCategories.Rows[0][ProductOptionCategoryDesc.ProductOptionCagegoryId]);
            productOptionCategory.Name                        = Convert.ToString(productOptionCategories.Rows[0][ProductOptionCategoryDesc.Name]);

            return productOptionCategory;
        }

        public static ProductOptionCategory GetProductOptionCategory(string name)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(ProductOptionCategoryDesc.Name, ConditionExpress.EQUALS, name);

            ProductOptionCategory[] set = Query(builder.ConditionString);

            if (set != null && set.Length > 0)
            {
                return set[0];
            }

            return null;
        }

        public static ProductOptionCategory[] Query(string condition)
        {
            // query factories from database
            string sql = "select * from " + ProductOptionCategoryDesc.TableName;
            if (condition != null && condition.Length > 0)
            {
                sql += " where " + condition;
            }
            DataTable table = DatabaseObject.Instance.ExecuteQuerySQL(sql);


            // rebuild the data
            if (table != null && table.Rows.Count > 0)
            {
                ProductOptionCategory[] productOptionCategories = new ProductOptionCategory[table.Rows.Count];

                for (int i = 0; i < table.Rows.Count; i++)
                {
                    ProductOptionCategory productOptionCategory = new ProductOptionCategory();

                    productOptionCategory.Id                             = Convert.ToInt32(table.Rows[i][ProductOptionCategoryDesc.Id]);
                    productOptionCategory.ProductOptionCagegoryId        = Convert.ToInt32(table.Rows[i][ProductOptionCategoryDesc.ProductOptionCagegoryId]);
                    productOptionCategory.Name                           = Convert.ToString(table.Rows[i][ProductOptionCategoryDesc.Name]);


                    productOptionCategories.SetValue(productOptionCategory, i);
                }

                return productOptionCategories;
            }
            return null;
        }

        public static Int32 Add(ProductOptionCategory productOptionCategory)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.InsertAdd(ProductOptionCategoryDesc.ProductOptionCagegoryId,          productOptionCategory.ProductOptionCagegoryId);
            builder.InsertAdd(ProductOptionCategoryDesc.Name,                             productOptionCategory.Name);

            builder.QueryTableName = ProductOptionCategoryDesc.TableName;

            string sql = builder.InsertCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }

        public static bool Delete(Int32 id)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(ProductOptionCategoryDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = ProductOptionCategoryDesc.TableName;
            string sql = builder.DeleteCommandLine;

            return (DatabaseObject.Instance.ExecuteNonQuerySQL(sql) != -1);
        }

        public static Int32 Update(ProductOptionCategory productOptionCategory)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.UpdateAdd(ProductOptionCategoryDesc.ProductOptionCagegoryId,           productOptionCategory.ProductOptionCagegoryId);
            builder.UpdateAdd(ProductOptionCategoryDesc.Name,                              productOptionCategory.Name);

            builder.ConditionAdd(ProductOptionCategoryDesc.Id, ConditionExpress.EQUALS, productOptionCategory.Id);
            builder.QueryTableName = ProductOptionCategoryDesc.TableName;

            string sql = builder.UpdateCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }

    }
}
